package Physics;

public class IntersectionTests {
	public static boolean sphereAndHalfSpace(CollisionSphere sphere, CollisionPlane plane)
	{
		/** Find the distance from the origin */
		float ballDistance = plane.m_direction.dotProduct(sphere.getAxis(3)) - sphere.m_radius;
		
		/** Check for the intersection */
		return (ballDistance <= plane.m_distance);
	}
	public static boolean sphereAndSphere(CollisionSphere one, CollisionSphere two)
	{
		/** Find the vector between the spheres */
		Vector3 midline = one.getAxis(3).subtract(two.getAxis(3));
		
		/** See if the magnitude is smaller than the radii combined */
		return(midline.squareMagnitude() < (one.m_radius + two.m_radius) * (one.m_radius + two.m_radius));
	}
	public static boolean boxAndBox(CollisionBox one, CollisionBox two)
	{
		Vector3 toCenter = two.getAxis(3).subtract(one.getAxis(3));
		Vector3 test;
		return (
				/** Check box one's axis first */
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(0), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(1), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(2), toCenter) &&
				/** Check box two's axis*/
				InlineFunctions.overlapOnAxis(one, two, two.getAxis(0), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, two.getAxis(1), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, two.getAxis(2), toCenter) &&
				/** Now check the cross products which gives us the
				 * remaining axes to check */
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(0).crossProduct(two.getAxis(0)), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(0).crossProduct(two.getAxis(1)), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(0).crossProduct(two.getAxis(2)), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(1).crossProduct(two.getAxis(0)), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(1).crossProduct(two.getAxis(1)), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(1).crossProduct(two.getAxis(2)), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(2).crossProduct(two.getAxis(0)), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(2).crossProduct(two.getAxis(1)), toCenter) &&
				InlineFunctions.overlapOnAxis(one, two, one.getAxis(2).crossProduct(two.getAxis(2)), toCenter)
				);
	}
	public static boolean boxAndHalfSpace(CollisionBox box, CollisionPlane plane)
	{
		/** Project the box onto the plane's axis */
		float projectedRadius = InlineFunctions.transformToAxis(box, plane.m_direction);
		/** Determine the distance from the box to the origin */
		float boxDistance = plane.m_direction.dotProduct(box.getAxis(3)) - projectedRadius;
		/** Check for intersection */
		return (boxDistance <= plane.m_distance);
	}
}
